package edu.zhangxu.dao;


import edu.zhangxu.util.JDBCUtils;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;

/*
 封装了针对表的通用操作。
 */
public abstract class BaseDao {
    // 增删改通用操作
    public int update(Connection con, String sql, Object... args) {
        PreparedStatement ps = null;
        try {
            ps = con.prepareStatement(sql);
            for(int i = 0;i < args.length;i++) {
                ps.setObject(i+1, args[i]);
            }
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeConnection(null, ps);
        }
        return -1;
    }

    // 查询通用操作，返回一条记录
    public <T> T getInstance(Connection con, Class<T> clazz, String sql, Object... args) {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = con.prepareStatement(sql);
            for(int i = 0;i < args.length;i++) {
                ps.setObject(i+1, args[i]);
            }
            rs = ps.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();
            int column = rsmd.getColumnCount();
            if(rs.next()) {
                T t = clazz.newInstance();
                for(int i = 0;i < column;i++) {
                    Object columnValue = rs.getObject(i+1);
                    String columnLabel = rsmd.getColumnLabel(i+1);
                    Field field = clazz.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(t, columnValue);
                }
                return t;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeConnection(null, ps, rs);
        }
        return null;
    }

    // 查询通用操作，返回多条记录
    public <T> List<T> getList(Connection con, Class<T> clazz, String sql, Object... args) {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = con.prepareStatement(sql);
            for(int i = 0;i < args.length;i++) {
                ps.setObject(i+1, args[i]);
            }
            rs = ps.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();
            int column = rsmd.getColumnCount();
            List<T> list = new ArrayList<>();
            while(rs.next()) {
                T t = clazz.newInstance();
                for(int i = 0;i < column;i++) {
                    Object columnValue = rs.getObject(i+1);
                    String columnLabel = rsmd.getColumnLabel(i+1);
                    Field field = clazz.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(t, columnValue);
                }
                list.add(t);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeConnection(null, ps, rs);
        }
        return null;
    }

    // 用于查询特殊值的方法
    public <E> E getValue(Connection con, String sql, Object... args) {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = con.prepareStatement(sql);
            for(int i = 0;i < args.length;i++) {
                ps.setObject(i+1, args[i]);
            }
            rs = ps.executeQuery();
            if(rs.next()) {
                return (E)rs.getObject(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeConnection(null, ps, rs);
        }
        return null;
    }
}
